package com.xxx.blog.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.Article;
import com.xxx.blog.query.ArticleQuery;
import com.xxx.blog.service.IArticleService;
import com.xxx.blog.vo.GdVo;
import com.xxx.system.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequiredArgsConstructor
@RequestMapping("/article")
@Api(tags = "文章接口类")
public class ArticleController {

    private final IArticleService articleService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public R<List<Article>> findAll() {
        List<Article> list = articleService.findAll();
        return R.ok(list);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个")
    public R<Article> findOne(@PathVariable Long id) {
        Article one = articleService.findOne(id);
        return R.ok(one);
    }

    @PostMapping
    @ApiOperation(value = "分页加条件查询")
    public PageInfo<Article> queryPage(@RequestBody ArticleQuery ArticleQuery) {

        List<Article> lists = articleService.queryPage(ArticleQuery);
        return new PageInfo<Article>(lists);//limit a,b
    }

    @PutMapping
    @ApiOperation(value = "根据id更新或增加")
    @PreAuthorize(name = "新增文章",sn="article:add")
    public R<Void> addOrUpdateArticle(@RequestBody Article Article) {
        try {
            if (Article.getId() == null || Article.getId() == 0) {
                articleService.addArticle(Article);
            } else {
                articleService.updateArticle(Article);
            }
        } catch (Exception e) {
            return R.fail("用户名已经存在");
        }

        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个")
    public R<Void> deleteArticle(@PathVariable Long id) {
        try {
            articleService.deleteArticle(id);
        } catch (Exception e) {
            return R.fail("删除失败");
        }
        return R.ok();
    }

    @PatchMapping//批量请求
    @ApiOperation(value = "批量删除")
    public R<Void> batchArticles(@RequestBody Long[] ids) {
        try {
            articleService.batchArticles(ids);
        } catch (Exception e) {
            return R.fail("删除失败");
        }
        return R.ok();
    }

    @GetMapping("/getByFileName/{fileName}")
    @ApiOperation(value = "查询单个")
    public Article getByFileName(@PathVariable String fileName) {
        Article article = articleService.getByFileName(fileName);
        return article;//alt enter生成返回值
    }

    @GetMapping("/gd")
    @ApiOperation(value = "查询归档集合数据")
    public List<GdVo> getGd() {
        List<GdVo> list = articleService.getGd();//假设法
        return list;//alt enter
    }

    @GetMapping("/ra/{id}")
    @ApiOperation(value = "得到相关文章的集合")
    public List<Article> getRa(@PathVariable Long id) {
        List<Article> list = articleService.getRa(id);//假设法
        return list;//alt enter
    }

    @GetMapping("/archiveDetail/{yearMonth}")
    @ApiOperation(value = "根据年月查询文章")
    public R<List<Article>> getYearMonth(@PathVariable("yearMonth") String yearMonth) {
        return R.ok(articleService.archiveDetail(yearMonth));//alt enter
    }
}
